﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Web;
using LeMigue.TeamPortal.Base.Mvc;


namespace LeMigue.TeamPortal.Base.Globalization
{
    public static class Recursos
    {

        private static ResourceManager _rm(string plugin)
        {
            ResourceManager rm;
            //qualquer poblema que tenha irá retornar o main como padrão
            string pluginFind = plugin;
            if (String.IsNullOrWhiteSpace(plugin))
                pluginFind = TeamPortalPlugins.Main;

            if (!TeamPortalPlugins.GerenteRecursos.ContainsKey(pluginFind))
            {
                if (pluginFind == TeamPortalPlugins.Main)
                    throw new KeyNotFoundException(String.Format("Não foi encontrado um recurso principal", plugin));
                else
                    throw new KeyNotFoundException(String.Format("Plugin '{0}' não contém recursos", plugin));
            }
            else
            {
                rm = TeamPortalPlugins.GerenteRecursos[pluginFind];
            }
            return rm;
        }
        private static string GetString(Assembly assembly, string expression, params object[] args)
        {
            string plugin = TeamPortalPlugins.PluginNameByAssembly(assembly);

            return GetString(plugin, expression, args);

        }
        public static string GetString(string plugin, string expression, params object[] args)
        {
            ResourceManager rm = _rm(plugin);
            string texto = rm.GetString(expression, CultureInfo.CurrentUICulture);

            if (texto == null)
            {
                if (plugin != TeamPortalPlugins.Main)
                {
                    rm = _rm(TeamPortalPlugins.Main);
                    texto = rm.GetString(expression, CultureInfo.CurrentUICulture);

                    if (texto == null)
                    {
                        throw new ArgumentNullException(String.Format("A chave: {0}, não foi encontrada no plugin {1} e nem no recurso principal", expression, plugin));
                    }
                }
                else
                {
                    throw new ArgumentNullException(String.Format("A chave: {0}, não foi encontrada no recurso principal", expression));
                }

            }

            return String.Format(texto, args);
        }
        public static string Resource(this HtmlHelper htmlhelper, string expression, params object[] args)
        {
            
            return GetString(htmlhelper.ViewContext.Controller.GetType().Assembly, expression, args);

        }

        public static string Resource(this IGlobalization modelView, string expression, params object[] args)
        {
            return GetString(modelView.GetType().Assembly, expression, args);
        }

        public static string Resource(this Controller controler, string expression, params object[] args)
        {

            return GetString(controler.GetType().Assembly, expression, args);
        }

        public static string Resource(this Assembly assembly, string expression, params object[] args)
        {
            return GetString(assembly, expression, args);
        }
    }


}
